const { querySqlForInvoice } = require("../utils")
const { getToken,decode }  = require("../utils/user-jwt")
const multiparty = require('multiparty');


//获取金额 和 开票日期
const getProperty = (req,res,next) =>{
    const { body } = req
    const sql = `select invoicePrice , startDate , invoiceCom from invoice_list where invoiceNum = "${body.invoice}"`
    querySqlForInvoice(sql).then(result =>{
        console.log('result',result)
        const { invoicePrice = '' , startDate = '' , invoiceCom = ''} = result[0] || result
        const data = {
            invoicePrice,
            startDate,
            invoiceCom
        }
        console.log(data)
        res.status(200).json(data)
    })
} 
//回执  
const getReceipt = (req,res,next) =>{
    const form = new multiparty.Form();
    form.uploadDir='./public/invoiceImg'
    console.log(req)
    form.parse(req, function(err, fields, files) {
        if (err) {
            console.log('err',err)
        } else {
            console.log(fields)
            const { invoiceNum,invoiceCom, endDate } = fields
            console.log('存好了',fields,files)
            const reciptImg = files.reciptImg[0].path.replace(/\\/g, "/")
            const signatureImg = files.signatureImg[0].path.replace(/\\/g, "/")
            const sql_update = `update invoice_list set invoiceCom = "${invoiceCom}" , endDate = "${endDate}" , reciptImg = "${reciptImg}" , signatureImg = "${signatureImg}" where invoiceNum = ${invoiceNum}`
            querySqlForInvoice(sql_update).then(result =>{
                    console.log(result)
                    res.status(200).json({
                        status:'200'
                    })
            })
        }
    })
    }
    //添加发票
    const addProperty = (req,res,next) =>{
        const { body } = req
        const { invoiceCom , invoiceNum , invoicePrice , startDate , openInvoiceCom} = body
        const numSql = `select * from invoice_list where invoiceNum = ${invoiceNum}`
        querySqlForInvoice(numSql).then(result =>{
            console.log(result)
            if(result.length > 0){
                res.status(422).json({
                    msg:'该发票号码已存在，将不再重新入库'
                })
               
            }else{
                const sql = `insert into invoice_list(invoiceNum,invoicePrice,startDate,invoiceCom,openInvoiceCom) values (${invoiceNum},${invoicePrice},"${startDate}","${invoiceCom}","${openInvoiceCom}")`
                querySqlForInvoice(sql).then(result =>{
                    console.log(result)
                    res.status(200).json({
                        status:'200'
                    }).catch(result =>{
                        res.status(400).json({
                            status:'400'
                        })
                      })
                    })
                }
                })
        
        } 
    const getTable = (req , res , next ) =>{
        const { query } = req
        console.log(query)
        const { pageSize , current , invoiceNum = ''} = query
        const num = pageSize * (current - 1) + ',' + pageSize * current 
        console.log(num)
        let sql
        let totalSql
        let total
        if(invoiceNum != ''){
            sql = `select * from invoice_list  WHERE invoiceNuM LIKE '%${invoiceNum}%' limit ${num}`
            totalSql = `select count(*) total from invoice_list WHERE invoiceNuM LIKE '%${invoiceNum}%'`
        }else{
            sql = `select * from invoice_list limit ${num}`
            totalSql = 'select count(*) total from invoice_list'
        }
        querySqlForInvoice(totalSql).then(res =>{
            total =  res[0].total
        })
        querySqlForInvoice(sql).then(result =>{
            for(let item of result){
                 item.status = item.endDate == null ? 'no' : 'yes' 
            }
            const resData = {
                total:total,
                data:result,
                success:true
            }
            res.status(200).json(resData)
        })
    }


module.exports = {
    getProperty,
    getReceipt,
    addProperty,
    getTable
}